﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Data.OleDb;
using Microsoft.ApplicationBlocks.Data;
namespace XBYTestManagement
{
    public partial class FrmSolutionSetting : KryptonForm
    {
        private int ID;//测试方案ID
        private int SolutionStepStatus=1;//O:新增 1：查看 2：编辑 3：删除
        private int StepContentStatus = 1;//O:新增 1：查看 2：编辑 3：删除
        private int MethodStatus = 1;//O:新增 1：查看 2：编辑 3：删除
        public FrmSolutionSetting(int id)
        {
            InitializeComponent();
            ID = id;
            this.txtStandar.StateDisabled.Back.Color1 = System.Drawing.SystemColors.ControlLightLight;
            
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
            //    e.RowBounds.Location.Y,
            //    dgvMethod.RowHeadersWidth - 4,
            //    e.RowBounds.Height);

            //TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            //    dgvMethod.RowHeadersDefaultCellStyle.Font,
            //    rectangle,
            //    dgvMethod.RowHeadersDefaultCellStyle.ForeColor,
            //    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void kryptonButton3_Click(object sender, EventArgs e)
        {
            try
            {
                //DataGridViewRow row = new DataGridViewRow();
                //row.CreateCells(this.dataGridView1);
               
                dataSet.Tables[0].Rows.InsertAt( dataSet.Tables[0].NewRow(),dgvMethod.SelectedRows[0].Index);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请选择一行");
            }
        }

        private void kryptonButton4_Click(object sender, EventArgs e)
        {
            try
            {

                foreach (DataGridViewRow dgvRow in dgvMethod.SelectedRows)
                {
                    dgvMethod.Rows.Remove(dgvRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void kryptonButton5_Click(object sender, EventArgs e)
        {
            FrmTestMethodList frmMethod = new FrmTestMethodList();
            frmMethod.ShowDialog();
        }
       

        private void FrmSolutionSetting_Load(object sender, EventArgs e)
        {
            GroupBoxA.Text = "测试方案"; 
            GroupBoxB.Text = "测试阶段";
            GroupBoxC.Text = "测试内容";
            GroupBoxD.Text = "测试方法";
            dgvStep.AutoGenerateColumns = false;
            dgvContent.AutoGenerateColumns = false;
            dgvMethod.AutoGenerateColumns = false;
            cmbStepBind();
            cmbCaseBind();
            cmbContentBind();
            cmbConditionBind();
            BindSolution();
            BindSolutionStep(true);
        }

        private void kryptonGroupBox1_Panel_Paint(object sender, PaintEventArgs e)
        {

        }

        
        private void cmbStepBind()
        {
            string sql = "select * from testStep ";
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sql, null);
            cmbStep.DataSource = ds.Tables[0].DefaultView;
            cmbStep.DisplayMember = "content";
            cmbStep.ValueMember = "ID";
            cmbStep.SelectedIndex = -1;
            
        }
        private void cmbCaseBind()
        {
            string sql = "select * from testCase ";
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sql, null);
            cmbCase.DataSource = ds.Tables[0].DefaultView;
            cmbCase.DisplayMember = "content";
            cmbCase.ValueMember = "ID";
            cmbCase.SelectedIndex = -1;
        }
        private void cmbContentBind()
        {
            string sql = "select * from testContent ";
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sql, null);
            cmbContent.DataSource = ds.Tables[0].DefaultView;
            cmbContent.DisplayMember = "content";
            cmbContent.ValueMember = "ID";
            cmbContent.SelectedIndex = -1;
        }
        private void cmbConditionBind()
        {
            string sql = "select * from testCondition ";
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sql, null);
            cmbCondition.DataSource = ds.Tables[0].DefaultView;
            cmbCondition.DisplayMember = "content";
            cmbCondition.ValueMember = "ID";
            cmbCondition.SelectedIndex = -1;
        }
        private void BindSolution()
        {
            string sql = "select solution.ID as SolutionID,SolutionName,ProductID,product.number as ProductNumber,ProductName from solution left join product on solution.productid=product.id where solution.id=@ID";
            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = new OleDbParameter();
            param[0].ParameterName = "@ID";
            param[0].Value = ID;
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sql, param);

            txtSolutionName.Text = ds.Tables[0].Rows[0]["SolutionName"].ToString();
            txtProductNum.Text = ds.Tables[0].Rows[0]["ProductNumber"].ToString();
            txtProductName.Text = ds.Tables[0].Rows[0]["ProductName"].ToString();
        }
        private void BindSolutionStep(bool isLoad)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            sb.Append("SolutionStep.ID as SolutionStepID,");
            sb.Append("SolutionStep.SolutionID as SolutionID,");
            sb.Append("SolutionStep.StepID as StepID,");
            sb.Append("testStep.Content as StepContent,");
            sb.Append("SolutionStep.seq as SolutionStepSeq,");
            sb.Append("SolutionStep.CaseGrade as CaseGrade,");
            sb.Append("SolutionStep.CaseID as CaseID,");
            sb.Append("testCase.Content as CaseContent");
            sb.Append(" from (((SolutionStep)");
            sb.Append(" left outer join testStep");
            sb.Append(" on");
            sb.Append(" SolutionStep.stepID=testStep.ID)");
            sb.Append(" left outer join testCase");
            sb.Append(" on SolutionStep.CaseID=testCase.ID)");
            sb.Append(" where SolutionStep.SolutionID=@solutionID");

            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = new OleDbParameter();
            param[0].ParameterName = "@solutionID";
            param[0].Value = ID;
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
            this.dgvStep.SelectionChanged -= new EventHandler(dgvStep_SelectionChanged);
            dgvStep.DataSource = ds.Tables[0].DefaultView;
            this.dgvStep.SelectionChanged += new EventHandler(dgvStep_SelectionChanged);
            if (ds.Tables[0].Rows.Count > 0 && isLoad)//onLoad时第一行选中
            {
                dgvStep.Rows[0].Selected = true;

                DataRow dr = ds.Tables[0].Rows[0];
                cmbStep.SelectedValue = dr["StepID"];
                cmbCase.SelectedValue = dr["CaseID"];
                cmbLevel.Text = dr["CaseGrade"].ToString();
                int solutionStepID = (int)dr["SolutionStepID"];
                StepContentBind(solutionStepID);
            }
            else 
            {
                if (dgvStep.Rows.Count == 0) return;

                dgvStep.Rows[dgvStep.Rows.Count-1].Selected = true;
                int last= ds.Tables[0].Rows.Count-1;
                DataRow dr = ds.Tables[0].Rows[last];
                cmbStep.SelectedValue = dr["StepID"];
                cmbCase.SelectedValue = dr["CaseID"];
                cmbLevel.Text = dr["CaseGrade"].ToString();
                int solutionStepID = (int)dr["SolutionStepID"];
                StepContentBind(solutionStepID);
            }
        }
        private void StepContentBind(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            sb.Append("StepContent.ID as StepContentID,");
            sb.Append("StepContent.SolutionStepID as SolutionStepID,");
            sb.Append("StepContent.ContentID as ContentID,");
            sb.Append("testContent.content as testContent,");
            sb.Append("StepContent.seq as StepContentSeq,");
            sb.Append("StepContent.MixItem as MixItem,");
            sb.Append("StepContent.MethodID as MethodID,");
            sb.Append("StepContent.ConditionID as ConditionID,");
            sb.Append("testCondition.Content as testCondition");
            sb.Append(" from ((((StepContent)");
            sb.Append(" left outer join SolutionStep");
            sb.Append(" on");
            sb.Append(" StepContent.SolutionStepID=SolutionStep.ID)");
            sb.Append(" left outer join testContent");
            sb.Append(" on StepContent.ContentID=testContent.ID)");
            sb.Append(" left outer join testCondition");
            sb.Append(" on StepContent.ConditionID=testCondition.ID)");
            sb.Append(" where StepContent.SolutionStepID=@solutionStepID");

            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = new OleDbParameter();
            param[0].ParameterName = "@solutionStepID";
            param[0].Value = id;
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
            this.dgvContent.SelectionChanged -= new EventHandler(dgvContent_SelectionChanged);
            dgvContent.DataSource = ds.Tables[0].DefaultView;
            this.dgvContent.SelectionChanged += new EventHandler(dgvContent_SelectionChanged);

            if (ds.Tables[0].Rows.Count > 0)
            {
                dgvContent.Rows[0].Selected = true;
                DataRow dr = ds.Tables[0].Rows[0];
                cmbContent.SelectedValue = dr["ContentID"];
                
                cmbMixItem.Text = dr["MixItem"].ToString();
                cmbCondition.SelectedValue = dr["ConditionID"];
                MethodBind((int)dr["StepContentID"]);
            }
            else
            {
                cmbContent.Text = "";
                cmbContent.SelectedIndex = -1;
                cmbContent.SelectedIndex = -1;
               
                cmbMixItem.Text = "";
                cmbMixItem.Text = "";
                cmbMixItem.SelectedIndex = -1;
                cmbCondition.Text = "";
                cmbCondition.SelectedIndex = -1;
                cmbCondition.SelectedIndex = -1;
                dgvMethod.DataSource = null;
                txtMethod.Text = "";
                txtStandar.Text = "";
                txtFile.Text = "";
            }
            
        }
        private void MethodBind(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            sb.Append(" StepContent.ID as StepContentID,");
            sb.Append(" StepContent.MethodID as MethodID,");
            sb.Append(" testMethodSteps.ID as MethodStepsID,");
            sb.Append(" testMethodSteps.seq as MethodStepsSeq,");
            sb.Append(" testMethodSteps.content as MethodStepsContent,");
            sb.Append(" testMethodSteps.standard as MethodStepsStandard,");
            sb.Append(" testMethodSteps.exampleFile as ExampleFile");
            sb.Append(" from StepContent,testMethod,testMethodSteps");
            sb.Append("   where StepContent.MethodID=testMethod.ID");
            sb.Append(" and");
            sb.Append(" testMethod.ID=testMethodSteps.methodID");
            sb.Append(" and");
            sb.Append(" StepContent.ID=@StepContentID");
            //sb.Append(" from (((StepContent)");
            //sb.Append(" inner join testMethos");
            //sb.Append(" on");
            //sb.Append(" StepContent.MethodID=testMethod.ID)");
            //sb.Append(" left outer join testMethodSteps");
            //sb.Append(" on testMethos.ID=testMethodSteps.methodID)");
            //sb.Append(" where StepContent.ID=@StepContentID");
            OleDbParameter[] param = new OleDbParameter[1];
            param[0] = new OleDbParameter();
            param[0].ParameterName = "@StepContentID";
            param[0].Value = id;
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
            dgvMethod.DataSource = ds.Tables[0].DefaultView;

            if (ds.Tables[0].Rows.Count > 0)
            {
                dgvMethod.Rows[0].Selected = true;
                DataRow dr = ds.Tables[0].Rows[0];
                txtMethod.Text = dr["MethodStepsContent"].ToString();
                txtStandar.Text = dr["MethodStepsStandard"].ToString();
                txtFile.Text = dr["ExampleFile"].ToString();
               
                

            }
            else
            {
                txtMethod.Text = "";
                txtStandar.Text = "";
                txtFile.Text = "";
               dgvMethod.DataSource = null;
            }
           


        }
        private void dgvStep_SelectionChanged(object sender, EventArgs e)
        {
            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
            cmbStep.SelectedValue = dr["StepID"];
            cmbCase.SelectedValue = dr["CaseID"];
            cmbLevel.Text = dr["CaseGrade"].ToString();
            int solutionStepID = (int)dr["SolutionStepID"];
            StepContentBind(solutionStepID);

        }
        private void dgvContent_SelectionChanged(object sender, EventArgs e)
        {

            DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
            cmbContent.SelectedValue = dr["ContentID"];
            cmbMixItem.Text = dr["MixItem"].ToString();
            cmbCondition.SelectedValue = dr["ConditionID"];
            MethodBind((int)dr["StepContentID"]);
        }

        

        private void dgvMethod_SelectionChanged(object sender, EventArgs e)
        {
            DataRowView dr = (DataRowView)dgvMethod.CurrentRow.DataBoundItem;
            txtMethod.Text = dr["MethodStepsContent"].ToString();
            txtStandar.Text = dr["MethodStepsStandard"].ToString();
            txtFile.Text = dr["ExampleFile"].ToString();
        }
        private void dgvContent_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvContent_DataSourceChanged(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SolutionStepStatus = 0;//新增
            cmbStep.Enabled = true;
            cmbCase.Enabled = true;
            cmbLevel.Enabled = true;
            cmbStep.SelectedIndex = -1;
            cmbStep.SelectedIndex = -1;
            cmbCase.SelectedIndex = -1;
            cmbCase.SelectedIndex = -1;
            cmbLevel.SelectedIndex = -1;
            btnAdd.Enabled = false;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnCancel.Enabled = true;
            this.dgvStep.SelectionChanged -= new EventHandler(dgvStep_SelectionChanged);

            foreach (DataGridViewRow row in dgvStep.Rows)
            {
                row.Selected = false;
            }
           // this.dgvStep.SelectionChanged += new EventHandler(dgvStep_SelectionChanged); 

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SolutionStepStatus = 1;//查看
            btnAdd.Enabled = true;
            btnSave.Enabled = false;
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnCancel.Enabled = false;
            this.dgvStep.SelectionChanged += new EventHandler(dgvStep_SelectionChanged); 
            if (dgvStep.Rows.Count>0)
            {
                dgvStep.Rows[0].Selected = true;

                DataRowView dr = (DataRowView)dgvStep.Rows[0].DataBoundItem;
                cmbStep.SelectedValue = dr["StepID"];
                cmbCase.SelectedValue = dr["CaseID"];
                cmbLevel.Text = dr["CaseGrade"].ToString();
                int solutionStepID = (int)dr["SolutionStepID"];
                StepContentBind(solutionStepID);
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (SolutionStepStatus==0)//新增
            //{
                if (cmbStep.SelectedIndex == -1)
                {
                    KryptonMessageBox.Show("请选择测试阶段","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    
                    cmbStep.Focus();
                    return;
                }
                if (cmbCase.SelectedIndex == -1)
                {
                    KryptonMessageBox.Show("请选择测试用例","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    cmbCase.Focus();
                    return;
                }
                if (cmbLevel.SelectedIndex == -1)
                {
                    KryptonMessageBox.Show("请选择用例等级","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    cmbLevel.Focus();
                    return;
                }
                try
                {
                    switch (SolutionStepStatus)
                    {
                        case 0://新增
                            AddStep();  
                            BindSolutionStep(false);
                            dgvStep.Rows[dgvStep.Rows.Count - 1].Selected = true;
                            break;
                        case 2://编辑
                            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
                            int currentID = (int)dr["SolutionStepID"];
                            EditStep();
                            BindSolutionStep(true);
                            foreach (DataGridViewRow row in dgvStep.Rows)
                            {
                                DataRowView r = (DataRowView)row.DataBoundItem;
                                if ((int)r["SolutionStepID"]==currentID)
                                    row.Selected = true;
                            }
                            break;
                    }
                    
                    //绑定测试步骤datagridview
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnDelete.Enabled = true;
                    btnEdit.Enabled = true;
                    btnCancel.Enabled = false;
                    cmbCase.Enabled = false;
                    cmbStep.Enabled = false;
                    cmbLevel.Enabled = false;
                    SolutionStepStatus = 1;
                }
                catch(Exception ex)
                {
                    KryptonMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            //}
        }
        private void AddStep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" insert into SolutionStep ( solutionID,StepID,seq,CaseGrade,CaseID) ");
            sb.Append(" values(@solutionID,@StepID,@seq,@CaseGrade,@CaseID)");
            OleDbParameter[] param = new OleDbParameter[5];
            param[0] = new OleDbParameter("@solutionID", ID);
            param[1] = new OleDbParameter("@StepID", cmbStep.SelectedValue);
            param[2] = new OleDbParameter("@seq", dgvStep.Rows.Count+1);
            param[3] = new OleDbParameter("@CaseGrade", cmbLevel.SelectedItem);
            param[4] = new OleDbParameter("@CaseID", cmbCase.SelectedValue);

            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
          
        }

        private void EditStep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" update SolutionStep ");
            sb.Append(" set StepID=@StepID,");
            sb.Append(" CaseGrade=@CaseGrade,CaseID=@CaseID ");
            sb.Append(" where ID=@ID");
            OleDbParameter[] param = new OleDbParameter[4];
            //param[0] = new OleDbParameter("@solutionID", ID);
            param[0] = new OleDbParameter("@StepID", cmbStep.SelectedValue);
            //param[1] = new OleDbParameter("@seq", dgvStep.Rows.Count + 1);
            param[1] = new OleDbParameter("@CaseGrade", cmbLevel.SelectedItem);
            param[2] = new OleDbParameter("@CaseID", cmbCase.SelectedValue);
            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
            param[3] = new OleDbParameter("@ID", dr["SolutionStepID"]);

            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);


        }

        private void DeleteStep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from SolutionStep where ID=@ID");
            OleDbParameter[] param = new OleDbParameter[1];
            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@ID", dr["SolutionStepID"]);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
        }
        
        private void btnEdit_Click(object sender, EventArgs e)
        {
            SolutionStepStatus = 2;//修改
            cmbStep.Enabled = true;
            cmbCase.Enabled = true;
            cmbLevel.Enabled = true;
            
            btnAdd.Enabled = false;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnCancel.Enabled = true;
            //this.dgvStep.SelectionChanged -= new EventHandler(dgvStep_SelectionChanged);

            //foreach (DataGridViewRow row in dgvStep.Rows)
            //{
            //    row.Selected = false;
            //}
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            
            if (dgvContent.Rows.Count > 0)
            {
                KryptonMessageBox.Show("请先删除测试内容然后删除测试阶段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SolutionStepStatus = 3;//删除
            DeleteStep();

        }

        private void btnCCancel_Click(object sender, EventArgs e)
        {
            StepContentStatus = 1;//查看
            btnCAdd.Enabled = true;
            btnCSave.Enabled = false;
            btnCDelete.Enabled = true;
            btnCEdit.Enabled = true;
            btnCCancel.Enabled = false;
            this.dgvContent.SelectionChanged += new EventHandler(dgvContent_SelectionChanged);
            if (dgvContent.Rows.Count > 0)
            {
                dgvStep.Rows[0].Selected = true;

                DataRowView dr = (DataRowView)dgvContent.Rows[0].DataBoundItem;
                cmbContent.SelectedValue = dr["ContentID"];
                cmbMixItem.Text = dr["MixItem"].ToString();
                cmbCondition.SelectedValue = dr["ConditionID"];
                MethodBind((int)dr["ContentID"]);
            }
        }

        private void btnCAdd_Click(object sender, EventArgs e)
        {
            StepContentStatus = 0;//新增
            cmbContent.Enabled = true;
            cmbMixItem.Enabled = true;
            cmbCondition.Enabled = true;
            cmbContent.SelectedIndex = -1;
            cmbContent.SelectedIndex = -1;
            cmbMixItem.SelectedIndex = -1;
            cmbCondition.SelectedIndex = -1;
            cmbCondition.SelectedIndex = -1;
            btnCAdd.Enabled = false;
            btnCSave.Enabled = true;
            btnCDelete.Enabled = false;
            btnCEdit.Enabled = false;
            btnCCancel.Enabled = true;
            this.dgvContent.SelectionChanged -= new EventHandler(dgvContent_SelectionChanged);

            foreach (DataGridViewRow row in dgvContent.Rows)
            {
                row.Selected = false;
            }
        }
        private void AddContent()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" insert into testMethod (Methodname) values ('')");
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(),null);
           
            sb.Clear();
            sb.Append(" select MAX(id) from  testMethod");
            var id=OleDbHelper.ExecuteScalar(OleDbHelper.connectionString, CommandType.Text, sb.ToString());
           
            sb.Clear();
            sb.Append(" insert into StepContent ( SolutionStepID,ContentID,seq,MixItem,ConditionID,methodID ) ");
            sb.Append(" values(@SolutionStepID,@ContentID,@seq,@MixItem,@ConditionID,@methodID)");
            OleDbParameter[] param = new OleDbParameter[6];

            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@SolutionStepID", dr["SolutionStepID"]);
            param[1] = new OleDbParameter("@ContentID", cmbContent.SelectedValue);
            param[2] = new OleDbParameter("@seq", dgvContent.Rows.Count + 1);
            param[3] = new OleDbParameter("@MixItem", cmbMixItem.SelectedItem);
            param[4] = new OleDbParameter("@ConditionID", cmbCondition.SelectedValue);
            param[5] = new OleDbParameter("@methodID", id);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);

        }
        private void EditContent()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" update StepContent ");
            sb.Append(" set SolutionStepID=@SolutionStepID,ContentID=@ContentID,MixItem=@MixItem,ConditionID=@ConditionID");
            sb.Append(" where ID = @ID");

            OleDbParameter[] param = new OleDbParameter[5];

            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@SolutionStepID", dr["SolutionStepID"]);
            param[1] = new OleDbParameter("@ContentID", cmbContent.SelectedValue);
            param[2] = new OleDbParameter("@MixItem", cmbMixItem.SelectedItem);
            param[3] = new OleDbParameter("@ConditionID", cmbCondition.SelectedValue);
            DataRowView row = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
            param[4] = new OleDbParameter("@ID", row["StepContentID"]);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
        }

        private void DeleteContent()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from StepContent where ID=@ID ");
            OleDbParameter[] param = new OleDbParameter[1];
            DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@ID", dr["StepContentID"]);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
        }

        private void btnCSave_Click(object sender, EventArgs e)
        {
            if (cmbContent.SelectedIndex == -1)
            {
                KryptonMessageBox.Show("请选择测试内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                cmbContent.Focus();
                return;
            }
            if (cmbMixItem.SelectedItem ==null)
            {
                KryptonMessageBox.Show("请选择测试用例", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbMixItem.Focus();
                return;
            }
            if (cmbCondition.SelectedIndex == -1)
            {
                KryptonMessageBox.Show("请选择测试条件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbMixItem.Focus();
                return;
            }
            try
            {
                switch (StepContentStatus)
                {
                    case 0://新增
                        AddContent();
                        DataRowView drv = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
                        StepContentBind((int)drv["SolutionStepID"]);
                        dgvContent.Rows[dgvContent.Rows.Count - 1].Selected = true;
                        dgvContent.FirstDisplayedScrollingRowIndex = dgvContent.Rows.Count - 1; 
                        DataRowView current = (DataRowView)dgvContent.Rows[dgvContent.Rows.Count - 1].DataBoundItem;
                        cmbContent.SelectedValue = current["ContentID"];
                        cmbMixItem.Text = current["MixItem"].ToString();
                        cmbCondition.SelectedValue = current["ConditionID"];
                        MethodBind((int)current["StepContentID"]);
                        break;
                    case 2://编辑
                        DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
                        int currentID = (int)dr["StepContentID"];
                        EditContent();
                        DataRowView row = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
                        StepContentBind((int)dr["SolutionStepID"]);
                        
                        foreach (DataGridViewRow item in dgvContent.Rows)
                        {
                            DataRowView r = (DataRowView)item.DataBoundItem;
                            if ((int)r["StepContentID"] == currentID)
                            {
                                item.Selected = true;
                                MethodBind((int)r["StepContentID"]);
                            }
                        }
                        break;
                }

                //绑定测试步骤datagridview
                btnCAdd.Enabled = true;
                btnCSave.Enabled = false;
                btnCDelete.Enabled = true;
                btnCEdit.Enabled = true;
                btnCCancel.Enabled = false;
                cmbContent.Enabled = false;
                cmbMixItem.Enabled = false;
                cmbCondition.Enabled = false;
                StepContentStatus = 1;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCEdit_Click(object sender, EventArgs e)
        {
            StepContentStatus = 2;//修改
            cmbContent.Enabled = true;
            cmbMixItem.Enabled = true;
            cmbCondition.Enabled = true;

            btnCAdd.Enabled = false;
            btnCSave.Enabled = true;
            btnCDelete.Enabled = false;
            btnCEdit.Enabled = false;
            btnCCancel.Enabled = true;
        }

        private void btnCDelete_Click(object sender, EventArgs e)
        {
            //if (dgvMethod.Rows.Count > 0)
            //{
            //    KryptonMessageBox.Show("请先删除测试方法然后删除测试内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            StepContentStatus = 3;//删除
            DeleteContent();
            DataRowView dr = (DataRowView)dgvStep.CurrentRow.DataBoundItem;
           
            int solutionStepID = (int)dr["SolutionStepID"];
            StepContentBind(solutionStepID);
        }

        private void btnDCancel_Click(object sender, EventArgs e)
        {
            MethodStatus = 1;//查看
            btnDAdd.Enabled = true;
            btnDSave.Enabled = false;
            btnDDelete.Enabled = true;
            btnDEdit.Enabled = true;
            btnDCancel.Enabled = false;
            txtFile.Enabled = false;
            txtMethod.Enabled = false;
            txtStandar.Enabled = false;
            this.dgvMethod.SelectionChanged += new EventHandler(dgvMethod_SelectionChanged);
            if (dgvMethod.Rows.Count > 0)
            {
                dgvMethod.Rows[0].Selected = true;

                DataRowView dr = (DataRowView)dgvMethod.Rows[0].DataBoundItem;
                txtMethod.Text = dr["MethodStepsContent"].ToString();
                txtStandar.Text = dr["MethodStepsStandard"].ToString();
                txtFile.Text = dr["ExampleFile"].ToString();
               
                //MethodBind((int)dr["ContentID"]);
            }
        }

        private void btnDAdd_Click(object sender, EventArgs e)
        {
            MethodStatus = 0;//新增
            txtMethod.Enabled = true;
            txtStandar.Enabled = true;
            txtFile.Enabled = true;
            txtMethod.Text = "";
            txtStandar.Text = "";
            txtFile.Text = "";
            btnDAdd.Enabled = false;
            btnDSave.Enabled = true;
            btnDDelete.Enabled = false;
            btnDEdit.Enabled = false;
            btnDCancel.Enabled = true;
            this.dgvMethod.SelectionChanged -= new EventHandler(dgvMethod_SelectionChanged);
            foreach (DataGridViewRow row in dgvMethod.Rows)
            {
                row.Selected = false;
            }
            this.dgvMethod.SelectionChanged += new EventHandler(dgvMethod_SelectionChanged);
        }
        private void AddMethodStep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" insert into testMethodSteps ( methodID,seq,content,standard,exampleFile) ");
            sb.Append(" values(@methodID,@seq,@content,@standard,@exampleFile)");
            OleDbParameter[] param = new OleDbParameter[5];

            DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@methodID", (int)dr["MethodID"]);
            param[2] = new OleDbParameter("@content", txtMethod.Text);
            param[1] = new OleDbParameter("@seq", dgvMethod.Rows.Count + 1);
            param[3] = new OleDbParameter("@standard", txtStandar.Text);
            param[4] = new OleDbParameter("@exampleFile", txtFile.Text);

            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);

        }
        private void EditMethodStep()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" update testMethodsteps ");
            sb.Append(" set Content=@Content,standard=@standard,exampleFile=@exampleFile");
            sb.Append(" where ID = @ID");

            OleDbParameter[] param = new OleDbParameter[4];

            DataRowView dr = (DataRowView)dgvMethod.CurrentRow.DataBoundItem;
           
            param[0] = new OleDbParameter("@Content", txtMethod.Text.Trim());
            param[1] = new OleDbParameter("@standard", txtStandar.Text.Trim());
            param[2] = new OleDbParameter("@exampleFile", txtFile.Text.Trim());
           
            param[3] = new OleDbParameter("@ID", dr["MethodStepsID"]);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
        }
        private void btnDSave_Click(object sender, EventArgs e)
        {
            if (txtMethod.Text.Trim() == "")
            {
                KryptonMessageBox.Show("请输入方法描述", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                txtMethod.Focus();
                return;
            }
            
            try
            {
                switch (MethodStatus)
                {
                    case 0://新增
                        AddMethodStep();
                        //DataRowView drv = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
                        //StepContentBind((int)drv["SolutionStepID"]);
                        DataRowView current = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
                        MethodBind((int)current["StepContentID"]);
                        dgvMethod.Rows[dgvMethod.Rows.Count - 1].Selected = true;
                        dgvMethod.FirstDisplayedScrollingRowIndex = dgvMethod.Rows.Count - 1;
                        DataRowView t = (DataRowView)dgvMethod.Rows[dgvMethod.Rows.Count - 1].DataBoundItem;
                        txtMethod.Text = t["MethodStepsContent"].ToString();
                        txtStandar.Text = t["MethodStepsStandard"].ToString();
                        txtFile.Text = t["ExampleFile"].ToString();
                        break;
                    case 2://编辑
                        
                        DataRowView drMethod = (DataRowView)dgvMethod.CurrentRow.DataBoundItem;

                        DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;
                        int currentID = (int)dr["StepContentID"];
                        EditMethodStep();
                        MethodBind(currentID);
                        foreach (DataGridViewRow item in dgvMethod.Rows)
                        {
                            DataRowView r = (DataRowView)item.DataBoundItem;
                            if ((int)r["MethodStepsID"] == (int)drMethod["MethodStepsID"])
                            {
                                item.Selected = true;
                                txtMethod.Text = r["MethodStepsContent"].ToString();
                                txtStandar.Text = r["MethodStepsStandard"].ToString();
                                txtFile.Text = r["ExampleFile"].ToString();
                            }
                        }
                        
                        break;
                }

                
                btnDAdd.Enabled = true;
                btnDSave.Enabled = false;
                btnDDelete.Enabled = true;
                btnDEdit.Enabled = true;
                btnDCancel.Enabled = false;
                txtMethod.Enabled = false;
                txtStandar.Enabled = false;
                txtFile.Enabled = false;
                MethodStatus = 1;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDDelete_Click(object sender, EventArgs e)
        {
            MethodStatus = 3;//删除
            DeleteMethod();
            DataRowView dr = (DataRowView)dgvContent.CurrentRow.DataBoundItem;

            int ContentID = (int)dr["ContentID"];
            MethodBind(ContentID);
        }
        private void DeleteMethod()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from testMethodSteps where ID=@ID");
            OleDbParameter[] param = new OleDbParameter[1];
            DataRowView dr = (DataRowView)dgvMethod.CurrentRow.DataBoundItem;
            param[0] = new OleDbParameter("@ID", dr["MethodStepsID"]);
            OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sb.ToString(), param);
        }

        private void btnDEdit_Click(object sender, EventArgs e)
        {
            MethodStatus = 2;//修改
            txtMethod.Enabled = true;
            txtStandar.Enabled = true;
            txtFile.Enabled = true;

            btnDAdd.Enabled = false;
            btnDSave.Enabled = true;
            btnDDelete.Enabled = false;
            btnDEdit.Enabled = false;
            btnDCancel.Enabled = true;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            FrmMethodSteps f = new FrmMethodSteps();
            f.ShowDialog();
            txtFile.Text = f.File;
            txtMethod.Text = f.Content;
            txtStandar.Text = f.Standard;
        }
    }
}
